package mxm.amfire;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;

import me.yokeyword.fragmentation.SupportFragment;
import mxm.amfire.event.StartFragEvent;

/**
 * Created by zengpeijin on 2017/2/13.
 */

public class LogoFragment extends SupportFragment {
    private View rootView;
    private RecyclerView mRecyclerView;

    private String titles[];
    private int res[];
    private String chinaStrs[] = {"比亚迪", "中国一汽","长安","东风","奇瑞","帝豪","吉利","英伦","众泰","夏利","奔腾","长城"};
    private int chinaRes[] = {R.drawable.byd, R.drawable.zhongguoyiqi,R.drawable.changan,R.drawable.dongfeng,R.drawable.qirui,R.drawable.dihao,R.drawable.jili,R.drawable.yinglun,R.drawable.zhongtai,R.drawable.xiali,R.drawable.benteng,R.drawable.changcheng};
    private String germanyStrs[] = {"大众", "宝马","奔驰","奥迪","保时捷","迈巴赫","欧宝","泰卡特","阿尔宾娜","特拉贝特","卡尔森","巴博斯"};
    private int germanyRes[] = {R.drawable.dazhong, R.drawable.baoma,R.drawable.benchi,R.drawable.aodi,R.drawable.baoshijie,R.drawable.maibahe,R.drawable.oubao,R.drawable.taikate,R.drawable.aerbingna,R.drawable.telabeite,R.drawable.kaersen,R.drawable.babosi};
    private String italyStrs[] = {"兰博基尼","法拉利"};
    private int italyRes[] = {R.drawable.lanbojini,R.drawable.falali};
    public static LogoFragment newInstance(int position) {
        LogoFragment fragment = new LogoFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("position",position);
        fragment.setArguments(bundle);
        return fragment;
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        int position= getArguments().getInt("position",0);
        if(position==0) {
            titles = chinaStrs;
            res = chinaRes;
        }else if(position==1){
            titles = germanyStrs;
            res = germanyRes;
        }else if(position==2){
            titles = italyStrs;
            res = italyRes;
        }else if(position==3){

        }else {

        }
        rootView = inflater.inflate(R.layout.fragment_logo, container,false);
        initView();
        return rootView;
    }

    private void initView() {
        mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerView);
        mRecyclerView.setLayoutManager(new GridLayoutManager(_mActivity, 4));
        mRecyclerView.setAdapter(new RecyclerView.Adapter<MyViewHolder>() {
            @Override
            public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                MyViewHolder myViewHolder = new MyViewHolder(LayoutInflater.from(_mActivity).inflate(R.layout.recycer_item_main, null, false));
                return myViewHolder;
            }

            @Override
            public void onBindViewHolder(MyViewHolder holder, int position) {
                holder.iv.setImageResource(res[position]);
                holder.tv.setText(titles[position]);
                holder.itemView.setOnClickListener(new MyOnClickListener(position));
            }

            @Override
            public int getItemCount() {
                return res.length;
            }

        });


    }

    private class MyOnClickListener implements View.OnClickListener {
        private int position;

        public MyOnClickListener(int position) {
            this.position = position;
        }

        @Override
        public void onClick(View view) {
            EventBus.getDefault().post(new StartFragEvent(LogoInfoFragment.newInstance(titles, res, position)));
        }
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        ImageView iv;
        TextView tv;

        public MyViewHolder(View view) {
            super(view);
            iv = (ImageView) view.findViewById(R.id.imageView);
            tv = (TextView) view.findViewById(R.id.textView);
        }
    }
}
